package com.yej.aiPolice.util

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.squareup.picasso.Picasso
import com.yej.aiPolice.R
import com.yej.aiPolice.util.TakePictureManager.takePictureCallBackListener
import java.io.File

class Fragment : Fragment(), View.OnClickListener {
    private var tvShow: TextView? = null
    private var ivShow: ImageView? = null
    private var takePictureManager: TakePictureManager? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_layout, null)
        initView(view)
        return view
    }

    private fun initView(view: View) {
        val btCamera =
            view.findViewById<Button>(R.id.btCamera)
        btCamera.setOnClickListener(this)
        val btAlbum = view.findViewById<Button>(R.id.btAlbum)
        btAlbum.setOnClickListener(this)
        tvShow = view.findViewById(R.id.tvShow)
        ivShow = view.findViewById(R.id.ivShow)
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.btCamera -> {
                takePictureManager = TakePictureManager(this)
                takePictureManager!!.setTailor(1, 1, 350, 350)
                takePictureManager!!.startTakeWayByCarema()
                takePictureManager!!.setTakePictureCallBackListener(object :
                    takePictureCallBackListener {
                    override fun successful(
                        isTailor: Boolean,
                        outFile: File,
                        filePath: Uri
                    ) {
                        tvShow!!.text = filePath.path
                        Picasso.with(activity).load(outFile).error(R.mipmap.ic_launcher)
                            .into(ivShow)
                    }

                    override fun failed(
                        errorCode: Int,
                        deniedPermissions: List<String>
                    ) {
                    }
                })
            }
            R.id.btAlbum -> {
                takePictureManager = TakePictureManager(this)
                takePictureManager!!.setTailor(1, 1, 350, 350)
                takePictureManager!!.startTakeWayByAlbum()
                takePictureManager!!.setTakePictureCallBackListener(object :
                    takePictureCallBackListener {
                    override fun successful(
                        isTailor: Boolean,
                        outFile: File,
                        filePath: Uri
                    ) {
                        tvShow!!.text = filePath.path
                        Picasso.with(activity).load(outFile).error(R.mipmap.ic_launcher)
                            .into(ivShow)
                    }

                    override fun failed(
                        errorCode: Int,
                        deniedPermissions: List<String>
                    ) {
                    }
                })
            }
        }
    }

    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        super.onActivityResult(requestCode, resultCode, data)
        takePictureManager!!.attachToActivityForResult(requestCode, resultCode, data)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        takePictureManager!!.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }
}